package com.yanze.schoolinformation.view.information.gank;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.view.View;

import com.example.xrecyclerview.XRecyclerView;
import com.yanze.schoolinformation.R;
import com.yanze.schoolinformation.adapter.rv.WanAndroidAdapter;
import com.yanze.schoolinformation.base.BaseFragment;
import com.yanze.schoolinformation.databinding.FragmentWanAndroidBinding;
import com.yanze.schoolinformation.databinding.HeaderWanAndroidBinding;
import com.yanze.schoolinformation.model.bean.WanAndroidBannerBean;
import com.yanze.schoolinformation.model.bean.WanAndroidListBean;
import com.yanze.schoolinformation.utils.CommonUtils;
import com.yanze.schoolinformation.utils.GlideImageLoader;
import com.yanze.schoolinformation.view.webview.WebViewActivity;
import com.yanze.schoolinformation.viewModule.gank.WanAndroidNavigator;
import com.yanze.schoolinformation.viewModule.gank.WanAndroidViewModel;
import com.youth.banner.listener.OnBannerListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by MrZheng on 2018/4/28.
 */

public class WanAndroidFragment extends BaseFragment<FragmentWanAndroidBinding> implements WanAndroidNavigator {

    private boolean mIsFirst = true;
    private boolean mIsPrepared = false;
    private WanAndroidViewModel viewModel;
    private WanAndroidAdapter mAdapter;
    private HeaderWanAndroidBinding headerBinding;

    @Override
    public int setContent() {
        return R.layout.fragment_wan_android;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //显示内容布局，初始化ViewModel，初始化RecyclerView
        viewModel = new WanAndroidViewModel(this);
        viewModel.setNavigator(this);
        initRecyclerView();

        mIsPrepared = true;
        /**
         * 因为启动时先走loadData()再走onActivityCreated，
         * 所以此处要额外调用load(),不然最初不会加载内容
         */
        loadData();
    }

    private void initRecyclerView() {
        bindingView.srlWanandroid.setColorSchemeColors(CommonUtils.getColor(R.color.colorTheme));
        bindingView.srlWanandroid.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                bindingView.srlWanandroid.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        viewModel.setPage(0);
                        viewModel.loadListData();//只刷新列表
                    }
                }, 1000);
            }
        });
        bindingView.xrvWanandroid.setLayoutManager(new LinearLayoutManager(getContext()));
        bindingView.xrvWanandroid.setPullRefreshEnabled(false);
        bindingView.xrvWanandroid.clearHeader();
        mAdapter = new WanAndroidAdapter(getActivity());
        bindingView.xrvWanandroid.setAdapter(mAdapter);
        headerBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.header_wan_android, null, false);
        bindingView.xrvWanandroid.addHeaderView(headerBinding.getRoot());
        viewModel.loadBannerData();

        bindingView.xrvWanandroid.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
            }

            @Override
            public void onLoadMore() {
                int page = viewModel.getPage();
                viewModel.setPage(++page);
                viewModel.loadListData();
            }
        });
    }

    @Override
    protected void loadData() {//第一次加载
        if (!mIsPrepared || !mIsVisible || !mIsFirst) {
            return;
        }
        bindingView.srlWanandroid.setRefreshing(true);
        bindingView.srlWanandroid.postDelayed(new Runnable() {
            @Override
            public void run() {
                viewModel.loadListData();
            }
        }, 500);
    }

    @Override
    public void showBannerView(ArrayList<String> bannerImages, ArrayList<String> mBannerTitle, final List<WanAndroidBannerBean.DataBean> result) {
        headerBinding.banner.setVisibility(View.VISIBLE);
        headerBinding.banner.setBannerTitles(mBannerTitle);
        headerBinding.banner.setImages(bannerImages).setImageLoader(new GlideImageLoader()).start();
        headerBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(int position) {
                if (result.get(position) != null && !TextUtils.isEmpty(result.get(position).getUrl())) {
                    WebViewActivity.loadUrl(getContext(), result.get(position).getUrl(), result.get(position).getTitle());
                }
            }
        });
    }

    @Override
    public void loadBannerFailure() {
        headerBinding.banner.setVisibility(View.GONE);
    }

    @Override
    public void loadHomeListFailure() {
        if (bindingView.srlWanandroid.isRefreshing()) {
            bindingView.srlWanandroid.setRefreshing(false);
        }
        if (mAdapter.getItemCount() == 0) {
            showError();
        } else {
            bindingView.xrvWanandroid.refreshComplete();
        }
    }

    @Override
    public void showAdapterView(WanAndroidListBean bean) {
        mAdapter.clear();
        mAdapter.addAll(bean.getData().getDatas());
        mAdapter.notifyDataSetChanged();
        bindingView.xrvWanandroid.refreshComplete();

        mIsFirst = false;
    }

    @Override
    public void refreshAdapter(WanAndroidListBean bean) {
        mAdapter.addAll(bean.getData().getDatas());
        mAdapter.notifyDataSetChanged();
        bindingView.xrvWanandroid.refreshComplete();
    }

    @Override
    public void showListNoMoreLoading() {
        bindingView.xrvWanandroid.noMoreLoading();
    }

    @Override
    public void showLoadSuccessView() {
        showContentView();
        bindingView.srlWanandroid.setRefreshing(false);
    }

    @Override
    protected void onRefresh() {
        bindingView.srlWanandroid.setRefreshing(true);
        viewModel.loadBannerData();
        viewModel.loadListData();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        viewModel.onDestory();
    }
}
